<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="楼层名称" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入楼层名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="客户端类型" prop="classify">
        <el-select v-model="queryParams.classify" placeholder="客户端类型" clearable>
          <el-option v-for="dict in dict.type.client_client_type" :key="dict.value" :label="dict.label"
            :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAddData"
          v-hasPermi="['system:notice:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdateData"
          v-hasPermi="['system:notice:edit']">修改</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="floorSetList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" width="200" />
      <el-table-column label="楼层名称" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="图标" align="center" prop="icon" :show-overflow-tooltip="true" />
      <el-table-column label="图片" align="center" prop="imageUrl" :show-overflow-tooltip="true" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdateData(scope.row)"
            v-hasPermi="['system:notice:edit']">修改</el-button>
            <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDeleteData(scope.row)"
            v-hasPermi="['system:notice:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="400px" append-to-body>
      <el-form ref="form" :model="form" label-width="90px">
        <el-form-item label="楼层名称" prop="title">
          <el-input v-model="form.title" />
        </el-form-item>
        <el-form-item label="图标" prop="icon">
          <el-input v-model="form.icon" />
        </el-form-item>
        <el-form-item label="图片" prop="imageUrl">
          <el-upload action="#" list-type="picture-card" :auto-upload="false" :on-remove="handleRemove"
            :on-preview="handlePictureCardPreview" :on-change="changeImg" ref="upload" >
            <i slot="default" class="el-icon-plus"></i>
            <div slot="file" slot-scope="{ file }">
              <img class="el-upload-list__item-thumbnail" :src="file.url" alt="" />
              <span class="el-upload-list__item-actions">
                <span class="el-upload-list__item-preview" @click="handlePictureCardPreview(file)">
                  <i class="el-icon-zoom-in"></i>
                </span>
                <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)">
                  <i class="el-icon-delete"></i>
                </span>
              </span>
            </div>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormData">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFloorSetData, delFloorSetData, addFloorSetData, updateFloorSetData, handleEnabledChange } from "@/api/mallAdmin/floorSet/data";
import axios from "axios";
import { getToken } from "@/utils/auth";


export default {
  name: "SuperviseUiIndex",
  dicts: ['client_client_type', 'app_floor_sign','app_promotion_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 协议表格数据
      floorSetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      opens: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        createBy: undefined,
        classify: undefined
      },
      // 表单参数
      form: {},
      dialogImageUrl: "",
      dialogVisible: false,
      disabled: false,
      imageFileList: undefined,
      headers: {
        Authorization: "Bearer " + getToken(),
        "Content-Type": "multipart/form-data"
      },
      // 表单校验
      rules: {
        title: [
          { required: true, message: "协议标题不能为空", trigger: "blur" }
        ],
        classify: [
          { required: true, message: "协议类型不能为空", trigger: "change" }
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询协议列表 */
    getList() {
      this.loading = true;
      listFloorSetData(this.queryParams).then(response => {
        this.floorSetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noticeId: undefined,
        noticeTitle: undefined,
        noticeType: undefined,
        noticeContent: undefined,
        status: "0"
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.noticeId)
      this.single = selection.length != 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAddData() {
      this.reset();
      this.open = true;
      this.title = "添加子楼层";
    },
    /** 修改按钮操作 */
    handleUpdateData(row) {
      this.reset();
      this.form = row;
      this.open = true;
      this.title = "修改楼层";
    },
    /** 修改启用状态*/
    handleEnabledChange(row) {
      let text = row.enabled === true ? "启用" : "禁用";
      this.$modal.confirm('确认"' + text + '""' + row.title + '"吗？').then(function () {
        return handleEnabledChange(row.id, row.enabled);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function () {
        row.enabled = !row.enabled;
      });
    },
    /** 提交按钮 */
    submitFormData: function () {
      if (this.imageFileList) {
        const loading = this.$loading({
          lock: true,
          text: '上传中...',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        });
        let formData = new FormData();
        formData.append('file', this.imageFileList[0].raw);
        axios.post(`${process.env.VUE_APP_BASE_API}/oss/common/upload`, formData, { headers: this.headers })
          .then(res => {
            if (res.status == 200) {
              let data = res.data
              console.log(data)
              if (data.code == 200) {
                this.$modal.msgSuccess("上传成功");
                this.form.imageUrl = data.data.url;
                if (this.form.id != undefined) {
                  updateFloorSetData(this.form.id, this.form).then((response) => {
                    this.$modal.msgSuccess("修改成功");
                    this.open = false;
                    this.getList();
                  });
                } else {
                  addFloorSetData(this.form).then((response) => {
                    this.$modal.msgSuccess("新增成功");
                    this.open = false;
                    this.getList();
                  });
                }
              } else {
                this.$message.error("上传文件失败" + res.data.msg);
              }
            }
          }).finally(res => {
            loading.close();
          });
        return;
      }
    },
    /** 删除按钮操作 */
    handleDeleteData(row) {
      const id = row.id || this.ids
      this.$modal.confirm('是否确认删除协议编号为"' + id + '"的数据项？').then(function() {
        return delFloorSetData(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    // 删除图片
    handleRemove(file, fileList) {
      const ul = document.querySelector('.el-upload-list--picture-card')
      ul.removeChild(ul.children[0])
      ul.nextSibling.style.display = 'block'
      console.log(file, fileList);
    },
    // 预览图片
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.raw.url;
      this.dialogVisible = true;
    },
    // 上传图片
    changeImg(file, fileList) {
      const ul = document.querySelector('.el-upload-list--picture-card')
      ul.nextSibling.style.display = 'none'
      console.log(file, fileList);
      this.imageFileList = fileList;
    }
  },


};
</script>
